<template>
  <base-btn
    :title="btn.transition_name"
    @submit="showMsgDialog"
    :type="type"
    @resetForm="resetForm"
  >
    <el-input
      v-model="form.suggestion"
      type="textarea"
      :autosize="{ minRows: 2, maxRows: 6 }"
      placeholder="请输入备注/意见"
      style="margin: 20px 0"
    >
    </el-input>
  </base-btn>
</template>
<script>
import BaseBtn from "@/components/workflows/baseBtn";
import workManagement from "@/api/workManagement/index";
export default {
  components: { BaseBtn },
  props: {
    id: {
      type: Number,
      default: 0
    },
    btn: {
      type: Object,
      default: function() {
        return {};
      }
    }
  },
  data() {
    return {
      form: { transition_id: "", suggestion: "" }
    };
  },
  computed: {
    type() {
      if (this.btn.attribute_type_id == 1) return "primary";
      else if (this.btn.attribute_type_id == 2) return "danger";
      else return "";
    }
  },
  methods: {
    toSubmit(callback) {
      this.form.transition_id = this.btn.transition_id;
      if (this.btn.in_add_node) {
        //加签完成
        workManagement
          .toAddNodeEnd(this.id, { suggestion: this.form.suggestion })
          .then(res => {
            this.msgSuccess("处理成功！");
            this.$emit("reload");
            callback && callback();
          });
      } else if (this.btn.is_accept) {
        //接单
        workManagement
          .toAccept(this.id, { suggestion: this.form.suggestion })
          .then(res => {
            this.msgSuccess("处理成功！");
            this.$emit("reload");
            callback && callback();
          });
      } else {
        //其它按钮事件
        workManagement.toTicketsDandle(this.id, this.form).then(res => {
          this.msgSuccess("处理成功！");
          this.$emit("reload");
          callback && callback();
        });
      }
    },
    resetForm() {
      this.form.suggestion = "";
    },
    showMsgDialog(callback) {
      if (this.btn.alert_enable) {
        let title = "确认操作？";
        if (this.btn.alert_text) title = this.btn.alert_text;
        this.$confirm(title, "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.toSubmit(callback);
        });
      } else {
        this.toSubmit(callback);
      }
    }
  }
};
</script>
